package com.example.doctor.jpa;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import com.example.common.entity.OnlineConsultation;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface OnlineConsultationRepository extends JpaRepository<OnlineConsultation, Long> {
    @Query(value = "select * from im_online_consultation where order_no_nvarchar = :OrderNo",nativeQuery = true)
    OnlineConsultation findByOrderNo(@Param("OrderNo") String OrderNo);

    @Query(value = "select COUNT(id_bigint) from im_online_consultation where dc_id_bigint = :DCID and pid_bigint = :pid and oc_status_tinyint = 1",nativeQuery = true)
    int HaveInquiry(@Param("DCID") long DCID, @Param("pid") long pid);

    @Query(value = "select count(1) from im_online_consultation where dc_id_bigint = :DCID and end_time_datetime <= now() and oc_status_tinyint = 1",nativeQuery = true)
    int haveInquiryEnd(@Param("DCID") long DCID);

    @Query(value = "select * from im_online_consultation where dc_id_bigint = :DCID and oc_status_tinyint = 1 and ((a_type_int in (10,11) and end_time_datetime < now()))",nativeQuery = true)
    List<OnlineConsultation> timeOutOrder(@Param("DCID") long DCID);

    @Transactional
    @Modifying
    @Query(value = "update im_online_consultation set oc_status_tinyint = 2,fact_finish_time_datetime = end_time_datetime where dc_id_bigint = :DCID and oc_status_tinyint = 1 and end_time_datetime <= now()",nativeQuery = true)
    void EndInquiry(@Param("DCID") long DCID);
}
